package com.ailight.smart.ai.controller;

import com.ailight.smart.ai.command.AiCommand;
import com.ailight.smart.ai.facade.AiModelFacade;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
import java.io.IOException;

/**
 * @author: 朱海鹏
 * @Package: com.ailight.smart.ai.controller
 * @Project: ai-light
 * @date: 2025/3/19 10:52
 * @description :
 */
@RestController
@RequestMapping(("/api/ai/model"))
@RequiredArgsConstructor
public class AiModelController {
    /**
     * 模型服务
     */
    
    private final AiModelFacade aiModelFacade;

    /**
     * 原生传统SSE模式
     *
     * @param command
     * @return
     */
    @PostMapping(value = "/get-model/answer")
    public Object getModelResult(@RequestBody AiCommand command) {
        // 传统sse模式
        return aiModelFacade.getModelAnswer(command);
    }


    /**
     * 模型非流式返回
     * @param msg
     * @return
     */
    @GetMapping(value = "/chat")
    public String streamChat(@RequestParam("msg") String msg) {
        return aiModelFacade.chat(msg);
    }

    /**
     * 测试...
     *
     * @return
     */
    @PostMapping("/sse")
    public Object streamSseMvc() {
        SseEmitter emitter = new SseEmitter(Long.MAX_VALUE);
        // 模拟发送数据
        new Thread(() -> {
            try {
                for (int i = 0; i < 10; i++) {
                    emitter.send(SseEmitter.event().data("Message " + i));
                    Thread.sleep(1000);
                }
                emitter.complete();
            } catch (IOException | InterruptedException e) {
                emitter.completeWithError(e);
            }
        }).start();

        return emitter;
    }

}
